﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Module.Category.BL;
using QLBH;
using QLBH.DTO;

namespace Module.Category
{
    public partial class ProductCatagoryManagement : ViewBase
    {
        public ProductCatagoryManagement()
        {
            InitializeComponent();
        }

        private void ProductCatagoryManagement_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = ProductCategoryManagementBL.Instance.GetAllProductCategories();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddProductCategoryDlg addproductCate = new AddProductCategoryDlg();
                if (addproductCate.ShowDialog()==DialogResult.OK)
                {                    
                    dataGridViewX1.DataSource = ProductCategoryManagementBL.Instance.GetAllProductCategories();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditProductCategory();
        }

        private void EditProductCategory()
        {
            try
            {
                EditProductCategotyDlg editProductCate = new EditProductCategotyDlg();
                editProductCate.ProductCategory = dataGridViewX1.CurrentRow.DataBoundItem as NhomHangDTO;

                if (editProductCate.ShowDialog() == DialogResult.OK)
                {
                    ProductCategoryManagementBL.Instance.UpdateProductCategory(editProductCate.ProductCategory);

                    dataGridViewX1.DataSource = ProductCategoryManagementBL.Instance.GetAllProductCategories();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<NhomHangDTO> selectedObjs = new List<NhomHangDTO>();
                foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
                {
                    selectedObjs.Add(row.DataBoundItem as NhomHangDTO);
                }

                if (selectedObjs.Count > 0)
                {
                    ProductCategoryManagementBL.Instance.DeleteProductCategories(selectedObjs);

                    dataGridViewX1.DataSource = ProductCategoryManagementBL.Instance.GetAllProductCategories();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = ProductCategoryManagementBL.Instance.GetAllProductCategories();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridViewX1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            EditProductCategory();
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }
    }
}
